package com.leenmvc.core.security.entity.core;

import com.leenmvc.core.annotation.DbInfo;
import com.leenmvc.core.annotation.FillField;
import com.leenmvc.core.annotation.PrimaryKey;
import com.leenmvc.core.annotation.TableField;
import com.leenmvc.core.annotation.validator.Exist;
import com.leenmvc.core.annotation.validator.Uniqueness;
import com.leenmvc.core.annotation.validator.ValueIn;
import com.leenmvc.core.annotation.validator.groups.AddGroup;
import com.leenmvc.core.annotation.validator.groups.UpdateGroup;
import com.leenmvc.core.base.BaseEntity;
import com.leenmvc.core.enums.FillType;
import com.leenmvc.core.security.dao.core.SysRoleUserDao;
import com.leenmvc.core.security.entity.export.SysUserExcel;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;

/**
 * 系统用户
 * NotEmpty 用在集合类上面
 * @NotBlank 用在String上面
 * @NotNull 用在基本类型上
 * @author leen
 */
@DbInfo(value = "sys_user", comment = "用户", excelEntity = SysUserExcel.class)
public class SysUser extends BaseEntity implements Serializable {

    @PrimaryKey
    @Exist(classes = {SysRoleUserDao.class}, fields = {"userId"})
    private Long id;

    /**
     * 用户名
     */
    @Uniqueness(message = "用户名重复，请换一个用户名", groups = {AddGroup.class, UpdateGroup.class})
    @NotBlank(message = "用户名不能为空", groups = {AddGroup.class})
    @Pattern(regexp = "^[a-zA-Z0-9]{4,16}$", message = "用户名只能是数字或字母", groups ={AddGroup.class,UpdateGroup.class} )
    private String username;
    /**
     * 密码
     */
    @NotBlank(message = "密码不能为空",groups = {AddGroup.class,UpdateGroup.class})
    @Pattern(regexp = "^.*(?=.*\\d)(?=.*[A-Z])(?=.*[a-z]).{6,16}", message = "密码要求为8-16位,包括大写字母,小写字母,数字", groups ={AddGroup.class} )
    private String password;

    /**
     * 姓名
     */
    @NotBlank(message = "真实姓名不能为空", groups = {AddGroup.class, UpdateGroup.class})
    private String realName;
    /**
     * 头像
     */
    private String headUrl;

    public SysUser() {
//        this.token = new SysUserToken();
    }

    /**
     * 性别   0：男   1：女    2：保密
     */
    @NotNull(message = "性别不可为空", groups = {UpdateGroup.class, AddGroup.class})
    @ValueIn(message = "性别的值为0或1", value = {"0", "1", "2"}, groups = {AddGroup.class, UpdateGroup.class})
    private Integer gender;

    /**
     * 邮箱
     */
    @Uniqueness(message = "邮箱重复，请换一个邮箱", groups = {AddGroup.class, UpdateGroup.class})
    @NotBlank(message = "邮箱不能为空",groups = {AddGroup.class, UpdateGroup.class})
    private String email;
    /**
     * 手机号
     */
    @Uniqueness(message = "手机号重复，请换一个手机号", groups = {AddGroup.class, UpdateGroup.class})
    @Pattern(regexp = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$",message="手机格式不正确",groups = {AddGroup.class,UpdateGroup.class})
    private String mobile;

    /**
     * 用户UUID，用于传入IOThub
     */
    @Uniqueness(message = "UUID重复，请检查", groups = {AddGroup.class})
    private String uuid;

    /**
     * 部门ID
     */
    private Long deptId;


    /**
     * 超级管理员   0：否   1：是
     */
    private Integer superAdmin;

    /**
     * 状态  0：停用   1：正常
     */
    private Integer status;

    /**
     * 角色id,绑定角色
     */
    private Long roleId;

    /**
     * 地址信息
     */
    @NotBlank(message = "地址不能为空",groups = {AddGroup.class, UpdateGroup.class})
    private String address;

    /**
     * 联系人
     */
    private String linkman;

    /**
     * 职务
     */
    private String duties;

    /**
     * 客户名称
     */
    private String customName;

    /**
     * 更新者
     */
    private Long updater;
    /**
     * 更新时间
     */
    private Date updateDate;


    protected Long creator;

    protected Date createDate;

    /**
     * 标识（用于角色绑定用户） 0 代表未绑定用户 1代表已绑定角色
     */
    @TableField(false)
    private Integer sign;


    public Integer getSign() {
        return sign;
    }

    public void setSign(Integer sign) {
        this.sign = sign;
    }

    public Long getCreator() {
        return creator;
    }

    public void setCreator(Long creator) {
        this.creator = creator;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    /**
     * 部门名称
     */
    @TableField(false)
    @FillField(entity = SysDept.class, outField = "name", inField = "deptId")
    private String deptName;

    /**
     * 该用户拥有的角色
     */
    @TableField(false)
    private Set<SysRole> roles;

    /**
     * 该用户拥有的菜单
     */
    @TableField(false)
    private Set<SysMenu> menus;

    /**
     * 该用户用由的权限列表
     */
    @TableField(false)
    private Set<String> permisions;

    /**
     *  该用户的权限信息
     */
    @TableField(false)
    private SysUserToken token;

    @TableField(false)
    @FillField(type = FillType.ONE_TO_ONE, entity = SysDept.class, inField = "deptId")
    private SysDept dept;

    public SysDept getDept() {
        return dept;
    }

    public void setDept(SysDept dept) {
        this.dept = dept;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getHeadUrl() {
        return headUrl;
    }

    public void setHeadUrl(String headUrl) {
        this.headUrl = headUrl;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public Long getDeptId() {
        return deptId;
    }

    public void setDeptId(Long deptId) {
        this.deptId = deptId;
    }

    public Integer getSuperAdmin() {
        return superAdmin;
    }

    public void setSuperAdmin(Integer superAdmin) {
        this.superAdmin = superAdmin;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getLinkman() {
        return linkman;
    }

    public void setLinkman(String linkman) {
        this.linkman = linkman;
    }

    public String getDuties() {
        return duties;
    }

    public void setDuties(String duties) {
        this.duties = duties;
    }

    public String getCustomName() {
        return customName;
    }

    public void setCustomName(String customName) {
        this.customName = customName;
    }

    public Long getUpdater() {
        return updater;
    }

    public void setUpdater(Long updater) {
        this.updater = updater;
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public SysUserToken getToken() {
        return token;
    }

    public void setToken(SysUserToken token) {
        this.token = token;
    }

    public Set<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(Set<SysRole> roles) {
        this.roles = roles;
    }

    public Set<SysMenu> getMenus() {
        return menus;
    }

    public void setMenus(Set<SysMenu> menus) {
        this.menus = menus;
    }

    public Set<String> getPermisions() {
        return permisions;
    }

    public void setPermisions(Set<String> permisions) {
        this.permisions = permisions;
    }

    public Long getRoleId() {
        return roleId;
    }

    public void setRoleId(Long roleId) {
        this.roleId = roleId;
    }
}
